package com.yj.suncity.user;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

import com.yj.suncity.R;
import com.yj.suncity.async.AsyncTaskUtil;
import com.yj.suncity.async.ThreadUtils;
import com.yj.suncity.user.bean.VersionBean;
import com.yj.suncity.utis.DialogUtils;
import com.yj.suncity.utis.Utils;

public class VersionUpdate
{
  private Context context;

  public VersionUpdate(Context paramContext)
  {
    this.context = paramContext;
  }

  public void Version(final String paramString)
  {
    new AsyncTaskUtil(this.context, null, null, false, null, false, "检查新版本......", new AsyncTaskUtil.ResultCallBack()
    {
      public void JSONResult(JSONObject paramAnonymousJSONObject)
      {
        final VersionBean localVersionBean = new VersionBean(paramAnonymousJSONObject);
        if (localVersionBean.getSuccess().equals("1"))
        {
          if (!localVersionBean.getVersion_number().equals(paramString))
          {
            new DialogUtils().backDialog(VersionUpdate.this.context, "有新版本，立即更新？", true, new DialogUtils.ReshActivity()
            {
              public void reshActivity()
              {
                Intent localIntent = new Intent(VersionUpdate.this.context, UpdateAPKServer.class);
                localIntent.putExtra("titleId", VersionUpdate.this.context.getResources().getString(R.string.app_name));
                localIntent.putExtra("url", localVersionBean.getApp_url());
                VersionUpdate.this.context.startService(localIntent);
              }
            });
            return;
          }
          new DialogUtils().backDialog(VersionUpdate.this.context, "暂无版本更新！", false, null);
          return;
        }
        Utils.showToast(VersionUpdate.this.context, localVersionBean.getMsg());
      }
    }).execute(new String[] { "http://www.ygtc168.com/provider/common.ashx?action=version" });
  }

  public void Version1(final String paramString)
  {
    new ThreadUtils().startThread("http://www.ygtc168.com/provider/common.ashx?action=version", new ThreadUtils.OnResultListener()
    {
      public void jsonResult(JSONObject paramAnonymousJSONObject)
      {
        final VersionBean localVersionBean = new VersionBean(paramAnonymousJSONObject);
        if ((localVersionBean.getSuccess().equals("1")) && (!localVersionBean.getVersion_number().equals(paramString)))
          new DialogUtils().backDialog(VersionUpdate.this.context, "有新版本，立即更新？", true, new DialogUtils.ReshActivity()
          {
            public void reshActivity()
            {
              Intent localIntent = new Intent(VersionUpdate.this.context, UpdateAPKServer.class);
              localIntent.putExtra("titleId", VersionUpdate.this.context.getResources().getString(R.string.app_name));
              localIntent.putExtra("url", localVersionBean.getApp_url());
              VersionUpdate.this.context.startService(localIntent);
            }
          });
      }
    });
  }

  public String getPresentVersion() {
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(
					context.getPackageName(), 0);
			return packageInfo.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
}